package com.onlyphoto.onlymall.mvp.view.binder

import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

import com.onlyphoto.onlymall.R
import com.bumptech.glide.Glide
import com.onlyphoto.onlymall.bean.response.MyBean
import com.onlyphoto.onlymall.listeners.NoDoubleClickListener
import com.onlyphoto.onlymall.mvp.view.activity.*
import com.onlyphoto.onlymall.utils.ConfigUtil

import me.drakeet.multitype.ItemViewBinder

/**
 * Created by gaochujia on 2019-07-01.
 */
class VerticalMenuBinder : ItemViewBinder<MyBean.MenuBean, VerticalMenuBinder.ViewHolder>() {


    override fun onCreateViewHolder(inflater: LayoutInflater, parent: ViewGroup): ViewHolder {
        val root = inflater.inflate(R.layout.vertical_menu, parent, false)
        return ViewHolder(root)
    }

    override fun onBindViewHolder(viewHolder: ViewHolder, menuBean: MyBean.MenuBean) {
        viewHolder.update(menuBean)
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        var menuName: TextView? = null
        var menuImg: ImageView? = null

        init {
            menuName = itemView.findViewById(R.id.menu_name)
            menuImg = itemView.findViewById(R.id.iv_menu)
        }

        fun update(menuBean: MyBean.MenuBean) {
            itemView.setOnClickListener(object : NoDoubleClickListener(){
                override fun onNoDoubleClick(view: View?) {
                    if (menuBean.id !=3 && !ConfigUtil.instance.loginStatus) {
                        itemView.context?.startActivity(Intent(itemView.context, LoginActivity::class.java))
                        return
                    }
                    when (menuBean.id) {
                        0 ->
                            itemView.context?.startActivity(Intent(itemView.context, OrderManagerActivity::class.java)
                                    .putExtra("status", 9))
                        1 ->
                            itemView.context?.startActivity(Intent(itemView.context, ShippingAddressActivity::class.java))
                        2 ->
                            itemView.context?.startActivity(Intent(itemView.context, CouponActivity::class.java))
                        3 ->
                            itemView.context?.startActivity(Intent(itemView.context, QuestionActivity::class.java))
                    }
                }
            })
            menuName?.text = menuBean.name
            Glide.with(menuImg!!.context).load(menuBean.image).placeholder(R.mipmap.img_default).into(menuImg!!)
        }
    }
}
